/*
    Android Asynchronous Http Client Sample
    Copyright (c) 2014 Marek Sebera <marek.sebera@gmail.com>
    https://github.com/android-async-http/android-async-http

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

package com.example.sample;


import com.ohos.volley.Request;
import com.ohos.volley.Response;
import com.ohos.volley.VolleyError;
import com.ohos.volley.toolbox.StringRequest;

public class PostSample extends SampleParentAbility {
    private static final String LOG_TAG = "PostSample";

    @Override
    public int getSampleTitle() {
        return ResourceTable.String_title_post_sample;
    }

    @Override
    public boolean isRequestBodyAllowed() {
        return true;
    }

    @Override
    public boolean isRequestHeadersAllowed() {
        return true;
    }

    @Override
    public Request<String> getRequest() {
        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.POST, getDefaultURL(),
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        debugStatusCode(LOG_TAG, 200);
                        debugResponse(LOG_TAG, response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if(error.networkResponse != null) {
                    debugStatusCode(LOG_TAG, error.networkResponse.statusCode);
                } else {
                    debugStatusCode(LOG_TAG, -1);
                }
                debugThrowable(LOG_TAG, error.getCause());
                debugResponse(LOG_TAG, error.getMessage() == null ? error.toString() : error.getMessage());
            }
        });
        return stringRequest;
    }

    @Override
    public String getDefaultURL() {
        return PROTOCOL + "httpbin.org/post";
    }
}

